package com.youyi.yyviewsdklibrary.View.shapeimageview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class BaseRoundImageView extends ImageView {
    private Paint paint;
    private Paint paint2;
    private int roundHeight;
    private int roundWidth;

    public BaseRoundImageView(Context context) {
        this(context, null);
    }

    public BaseRoundImageView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public BaseRoundImageView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.roundWidth = 0;
        this.roundHeight = 0;
        init(context);
    }

    private void init(Context context) {
        Paint paint3 = new Paint();
        this.paint = paint3;
        paint3.setColor(-1);
        this.paint.setAntiAlias(true);
        this.paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        Paint paint4 = new Paint();
        this.paint2 = paint4;
        paint4.setXfermode(null);
    }

    public void setRound(int i) {
        if (i > getWidth() / 2 || i > getHeight() / 2) {
            if (getWidth() > getHeight()) {
                i = getHeight() / 2;
            } else {
                i = getWidth() / 2;
            }
        }
        this.roundHeight = i;
        this.roundWidth = i;
        invalidate();
    }

    public void draw(Canvas canvas) {
        Bitmap createBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas2 = new Canvas(createBitmap);
        super.draw(canvas2);
        drawLiftUp(canvas2);
        drawLiftDown(canvas2);
        drawRightUp(canvas2);
        drawRightDown(canvas2);
        canvas.drawBitmap(createBitmap, 0.0f, 0.0f, this.paint2);
        createBitmap.recycle();
    }

    private void drawLiftUp(Canvas canvas) {
        Path path = new Path();
        path.moveTo(0.0f, (float) this.roundHeight);
        path.lineTo(0.0f, 0.0f);
        path.lineTo((float) this.roundWidth, 0.0f);
        path.arcTo(new RectF(0.0f, 0.0f, (float) (this.roundWidth * 2), (float) (this.roundHeight * 2)), -90.0f, -90.0f);
        path.close();
        canvas.drawPath(path, this.paint);
    }

    private void drawLiftDown(Canvas canvas) {
        Path path = new Path();
        path.moveTo(0.0f, (float) (getHeight() - this.roundHeight));
        path.lineTo(0.0f, (float) getHeight());
        path.lineTo((float) this.roundWidth, (float) getHeight());
        path.arcTo(new RectF(0.0f, (float) (getHeight() - (this.roundHeight * 2)), (float) (this.roundWidth * 2), (float) getHeight()), 90.0f, 90.0f);
        path.close();
        canvas.drawPath(path, this.paint);
    }

    private void drawRightDown(Canvas canvas) {
        Path path = new Path();
        path.moveTo((float) (getWidth() - this.roundWidth), (float) getHeight());
        path.lineTo((float) getWidth(), (float) getHeight());
        path.lineTo((float) getWidth(), (float) (getHeight() - this.roundHeight));
        path.arcTo(new RectF((float) (getWidth() - (this.roundWidth * 2)), (float) (getHeight() - (this.roundHeight * 2)), (float) getWidth(), (float) getHeight()), 0.0f, 90.0f);
        path.close();
        canvas.drawPath(path, this.paint);
    }

    private void drawRightUp(Canvas canvas) {
        Path path = new Path();
        path.moveTo((float) getWidth(), (float) this.roundHeight);
        path.lineTo((float) getWidth(), 0.0f);
        path.lineTo((float) (getWidth() - this.roundWidth), 0.0f);
        path.arcTo(new RectF((float) (getWidth() - (this.roundWidth * 2)), 0.0f, (float) getWidth(), (float) ((this.roundHeight * 2) + 0)), -90.0f, 90.0f);
        path.close();
        canvas.drawPath(path, this.paint);
    }
}
